What is the output of the following code snippet?int i = 0;while (i &l...
The while loop starts with 'i' initialized to 0 and increments 'i' by 1 in each iteration. If 'i' is even, it skips the remaining statements using the continue statement. Therefore, only odd values of 'i' are printed.
What is the output of the following code snippet?int i = 0;while (i &l...
Explanation:
- The code snippet initializes a variable i to 0 and enters a while loop that runs as long as i is less than 5.
- Inside the loop, there is an if statement that checks if i is an even number (i % 2 == 0), in which case it increments i by 1 and continues to the next iteration of the loop.
- If i is an odd number, it prints the value of i followed by a space.
- Finally, i is incremented by 1 at the end of each iteration of the loop.
Step-by-step Execution:
1. i = 0, i is even, so increment i to 1 and continue.
2. i = 1, i is odd, print 1.
3. i = 2, i is even, so increment i to 3 and continue.
4. i = 3, i is odd, print 3.
5. i = 4, i is even, so increment i to 5 and continue.
6. i = 5, loop exits as i is no longer less than 5.
Output:
1 3 5
Therefore, the output of the code snippet is "1 3 5".